import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class StoreController {

	private StoreView sv;
	private StoreModel sm;
	private MainPlayerController mp;
	private InventoryController ic;
	private boolean hasUserPressedBuy;
	
	public StoreController(InventoryController ic, MainPlayerController mp, StoreView sv, StoreModel sm)
	{
		this.sv = sv;
		this.mp = mp;
		this.sm = sm;
		this.ic = ic;
		hasUserPressedBuy = false;
		sv.addUserActionListener(new userActionListener());
	}
	
	public boolean hasUserPressedBuy()
	{
		return hasUserPressedBuy;
	}
	
	private class userActionListener implements ActionListener
	{	
		public void actionPerformed(ActionEvent e) 
		{
			int i = 0;
			while(i < sv.getTextFields().length && e.getSource() != sv.getTextFields()[i]) 	
				i++;
			
			if(i < sv.getTextFields().length)
			{
				int quantity = Integer.parseInt(sv.getTextFields()[i].getText());
				mp.addCash(sm.getPrice(i) * quantity);
				
				ic.incrementQuantityOfItem(sm.getType(i), quantity);
				sv.setLabel(i, "" + ic.getQuantityOfItem(sm.getType(i)));
			}
			else
				hasUserPressedBuy = true;
		}

	}
	
}
